package com.tfswx.ocrengine.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * @author Ricky
 * @date 2020/12/8
 */
public class Base64Util {
    public static String getBase64String(String filePath) {
        return getBase64String(new File(filePath));
    }

    public static String getBase64String(File file) {
        try {
            byte[] bytes = FileUtils.readFileToByteArray(file);
            return getBase64String(bytes);
        } catch (IOException e) {
            throw new RuntimeException("获取base64失败-->" + e.getMessage(), e);
        }
    }

    public static String getBase64String(byte[] bytes) {
        return Base64.encodeBase64String(bytes);
    }

    // 
    public static byte[] getByteArray(String base64String) {
        base64String = base64String.replaceAll("data:[^/]+/[^/;]+;base64,", "");
        return Base64.decodeBase64(base64String);
    }

    public static void main(String[] args) {
        String s = "";
        getByteArray(s);
    }
}
